package com.allm.ai.common.factory;

import java.util.List;

import org.springframework.stereotype.Component;

import com.allm.ai.common.strategy.AiModelStrategy;
import com.allm.ai.common.strategy.impl.GenericAIStrategy;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@RequiredArgsConstructor
public class AiModelStrategyFactory {
    
    private final List<AiModelStrategy> strategies;
    
    public AiModelStrategy getStrategy(String serviceProvider) {
        // 首先尝试找到匹配的策略
        for (AiModelStrategy strategy : strategies) {
            if (strategy.supports(serviceProvider) && !(strategy instanceof GenericAIStrategy)) {
                log.debug("找到匹配的策略: {} for {}", strategy.getClass().getSimpleName(), serviceProvider);
                return strategy;
            }
        }
        
        // 如果没有找到匹配的策略，返回通用策略
        for (AiModelStrategy strategy : strategies) {
            if (strategy instanceof com.allm.ai.common.strategy.impl.GenericAIStrategy) {
                log.debug("使用通用策略 for {}", serviceProvider);
                return strategy;
            }
        }
        
        // 如果连通用策略都没有，抛出异常
        throw new IllegalStateException("No strategy found for service provider: " + serviceProvider);
    }
}